﻿//********************************************************************************//
//创建日期：<创建日期，2016-02-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：银行资料
//********************************************************************************//

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui.YongHuGL
{
    /// <summary>
    /// 银行资料
    /// </summary>
    public partial class YinHangZL : BasePage
    {
        #region 属性

        /// <summary>
        /// gridView主键
        /// </summary>
        private int YONGHUXXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_YONGHUXXID"]);
            }
            set
            {
                ViewState["_YONGHUXXID"] = value;
            }
        }

        /// <summary>
        /// 记录当前查询的水表号,保存在dictionary中,方便实现查询内容记忆功能
        /// </summary>
        private Dictionary<string, string> yonghuBHS
        {
            get
            {
                if (ViewState["yonghuBHS"] == null)
                {
                    ViewState["yonghuBHS"] = new Dictionary<string, string>();

                }
                return ViewState["yonghuBHS"] as Dictionary<string, string>;
            }
        }

        /// <summary>
        /// 记录合同号
        /// </summary>
        private string HETONGH
        {
            get
            {
                return Convert.ToString(ViewState["HETONGH"]);
            }
            set
            {
                ViewState["HETONGH"] = value;
            }
        }

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 用户信息对象
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
                Display();
            }
            gvList.DataSource = dtSource;
            gvList.DataBind();
        }

        /// <summary>
        /// 重写Render方法
        /// </summary>
        /// <param name="sender"></param>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

        /// <summary>
        /// 左侧列表焦点变更事件
        /// </summary>
        protected void gv_FocusedRowChanged(object sender, EventArgs e)
        {
            if (gvList.FocusedRowIndex == -1)
            {
                return;
            }
            object id = gvList.GetRowValues(gvList.FocusedRowIndex, "ID");
            if (id != null)
            {
                YONGHUXXID = Convert.ToInt32(id);
                SetData();
            }
            else
            {
                YONGHUXXID = -1;
            }
        }

        /// <summary>
        /// grid修改
        /// </summary>
        protected void lnbModify_Command(object sender, CommandEventArgs e)
        {
            YONGHUXXID = Convert.ToInt32(e.CommandArgument);
            Status = GridViewStatus.Update;
            Display();
            SetData();
        }

        /// <summary>
        /// grid删除
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            YONGHUXXID = Convert.ToInt32(e.CommandArgument);
            _YS_YG_YONGHUXX = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXX(YONGHUXXID);
            //清除该水表对应的编号记录
            yonghuBHS.Remove(_YS_YG_YONGHUXX.S_YONGHUH);
            //清除结果集中对应记录
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                if (dtSource.Rows[i]["S_YONGHUH"].ToString() == _YS_YG_YONGHUXX.S_YONGHUH)
                {
                    dtSource.Rows.RemoveAt(i);
                }
            }
            //清空右侧内容区域,重新绑定
            InitData();
            gvList.DataSource = dtSource;
            gvList.DataBind();
            if (dtSource.Rows.Count > 0)
            {
                gvList.FocusedRowIndex = -1;
                gvList.FocusedRowIndex = 0;
            }
        }

        /// <summary>
        /// grid添加
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// grid放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Status = GridViewStatus.Init;
            Display();
            //如果当前有操作的记录,放弃则重新加载记录信息
            if (YONGHUXXID > 0)
            {
                SetData();
            }
        }

        /// <summary>
        /// 查询按钮事件
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtConYongHuH.Text.Trim() == "")
            {
                return;
            }
            Status = GridViewStatus.Init;
            LoadData(true);
            Display();
        }

        /// <summary>
        /// grid保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                SaveData();
            }
        }

        /// <summary>
        /// 清空列表中记录
        /// </summary>
        protected void btnClear_Click(object sender, EventArgs e)
        {
            yonghuBHS.Clear();
            dtSource = null;
            gvList.DataSource = null;
            gvList.DataBind();
            InitData();
        }

        /// <summary>
        /// 清除当前选中项外的其他记录
        /// </summary>
        protected void btnHoldSelected_Click(object sender, EventArgs e)
        {
            if (dtSource == null || dtSource.Rows.Count == 0)
            {
                return;
            }
            //取出未选中的行
            List<DataRow> rows = new List<DataRow>(); ;
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                if (Convert.ToInt32(dtSource.Rows[i]["ID"]) != YONGHUXXID)
                {
                    rows.Add(dtSource.Rows[i]);
                }
            }
            //移除未选中的行
            for (int i = 0; i < rows.Count; i++)
            {
                dtSource.Rows.Remove(rows[i]);
            }
            //绑定
            gvList.DataSource = dtSource;
            gvList.DataBind();
            if (dtSource.Rows.Count > 0)
            {
                gvList.FocusedRowIndex = -1;
                gvList.FocusedRowIndex = 0;
            }
        }



        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "gethth")
            {
                cr.SpecialIndetity = BusinessTool.GetHeTongH(txtYongHuH.Text);
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                e.Result = cr.ToString();
                return;
            }
            else if (e.Parameter == "newhth")
            {
                string newHeTongHao = txtHeTongH.Text.Trim();
                if (newHeTongHao == "")
                {
                    newHeTongHao = BusinessTool.GetHeTongH(txtYongHuH.Text);
                }
                else
                {
                    newHeTongHao = newHeTongHao.Replace(txtYongHuH.Text, "");
                    string newHTHPre = newHeTongHao.Substring(0, newHeTongHao.Length - 6);
                    string newHTHMid = newHeTongHao.Substring(newHeTongHao.Length - 6, 6);
                    newHTHMid = "0" + (Convert.ToInt32(newHTHMid) + 100001).ToString().Substring(1, 5);
                    newHeTongHao = newHTHPre + newHTHMid + txtYongHuH.Text;
                }
                cr.SpecialIndetity = newHeTongHao.ToString();
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                e.Result = cr.ToString();
                return;
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {

        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            txtCaiWuDZ.Text = "";
            txtCaiWuYB.Text = "";
            txtHeTongH.Text = "";
            txtKaiHuHM.Text = "";
            txtKaiHuYH.Text = "";
            txtLianXiBM.Text = "";
            txtLianXiDH.Text = "";
            txtLianXiR.Text = "";
            txtYHZH.Text = "";
            txtYongHuDZ.Text = "";
            txtYongHuH.Text = "";
            txtYONGHUM.Text = "";
            txtYouBian.Text = "";
            cbTuoShou.Checked = false;            
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData(bool alert)
        {
            //获取当前操作人所属站点
            string zhandianBH = BusinessTool.ZHANDIANBH();
            //连接要查询的用户号
            string bhs = "";
            foreach (var item in yonghuBHS.Keys)
            {
                bhs = bhs + item + ",";
            }

            if (txtConYongHuH.Text != "")
            {
                //判断是否存在,不存在弹提示
                DataRowCollection drc = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH like '%{0}%' and I_JLZT=0 and I_LEIXING=1 and {1}", txtConYongHuH.Text, zhandianBH == "00" ? "1=1" : "S_ZHANDIANBH=" + zhandianBH), null).Rows;
                if (drc.Count == 0)
                {
                    PageTools.AlertMsg(this, "该用户不存在,请重新输入!");
                    return;
                }
                //判断查询的水表号是否已经存在记忆列表中,不存在则添加
                if (!yonghuBHS.ContainsKey(txtConYongHuH.Text))
                {
                    yonghuBHS.Add(txtConYongHuH.Text, txtConYongHuH.Text);
                    bhs = bhs + txtConYongHuH.Text + ",";
                }
            }
            if (bhs == "")
            {
                return;
            }
            else
            {
                bhs = "," + bhs;
            }
            //查询
            DataTable dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("instr('{0}',','||S_YONGHUH||',',1,1)>0 and I_JLZT=0 and I_LEIXING=1", bhs), string.Format("instr('{0}',','||S_YONGHUH||',',1,1)", bhs));
            dtSource = dt;
            gvList.DataSource = dtSource;
            gvList.DataBind();
            //定位刚查询的数据
            if (dtSource.Rows.Count > 0)
            {
                PageTools.FocusOpreateRecord(dtSource, gvList, "S_YONGHUH", txtConYongHuH.Text, true);
            }
        }


        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (Status)
            {
                case GridViewStatus.Init:
                    gvList.Enabled = true;
                    gvList.Columns[2].SetColVisible(true);
                    gvList.Columns[3].SetColVisible(true);
                    SetText(false);
                    panListBtns.Enabled = true;
                    btnSave.Visible = false;
                    btnCancel.Visible = false;                    
                    break;
                case GridViewStatus.Search:
                    break;
                case GridViewStatus.Update:
                    gvList.Enabled = false;
                    gvList.Columns[2].SetColVisible(false);
                    gvList.Columns[3].SetColVisible(false);
                    SetText(true);
                    panListBtns.Enabled = false;
                    btnSave.Visible = true;
                    btnCancel.Visible = true;                    
                    txtYONGHUM.Focus();
                    break;
                case GridViewStatus.Add:
                    break;
            }
        }


        /// <summary>
        /// 设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
            txtYongHuH.ClientEnabled = false;
            txtYONGHUM.ClientEnabled = false;
            txtYongHuDZ.ClientEnabled = false;
            txtLianXiR.ClientEnabled = false;
            txtLianXiDH.ClientEnabled = false;
            txtLianXiBM.ClientEnabled = false;
            txtYouBian.ClientEnabled = false;
            txtKaiHuHM.ClientEnabled = false;
            txtYHZH.ClientEnabled = false;
            txtKaiHuYH.ClientEnabled = false;
            txtHeTongH.ClientEnabled = b;
            cbTuoShou.ClientEnabled = false;
            btnNewHeTongH.ClientEnabled = false;
            txtCaiWuDZ.ClientEnabled = b;
            txtCaiWuYB.ClientEnabled = b;
        }

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            //获取当前操作用户信息
            _YS_YG_YONGHUXX = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXX(YONGHUXXID);
            txtCaiWuDZ.Text = _YS_YG_YONGHUXX.S_CAIWUDZ;
            txtCaiWuYB.Text = _YS_YG_YONGHUXX.S_CAIWUYB;
            txtHeTongH.Text = BusinessTool.GetHeTongH(_YS_YG_YONGHUXX.S_YONGHUH);
            txtKaiHuHM.Text = _YS_YG_YONGHUXX.S_KAIHUHM;
            txtKaiHuYH.Text = _YS_YG_YONGHUXX.S_KAIHUYH;
            txtLianXiBM.Text = _YS_YG_YONGHUXX.S_LIANXIBM;
            txtLianXiDH.Text = _YS_YG_YONGHUXX.S_LIANXIDH;
            txtLianXiR.Text = _YS_YG_YONGHUXX.S_LIANXIR;
            txtYHZH.Text = _YS_YG_YONGHUXX.S_YINHANGZH;
            txtYongHuDZ.Text = _YS_YG_YONGHUXX.S_DIZHI;
            txtYongHuH.Text = _YS_YG_YONGHUXX.S_YONGHUH;
            txtYONGHUM.Text = _YS_YG_YONGHUXX.S_YONGHUM;
            txtYouBian.Text = _YS_YG_YONGHUXX.S_YOUBIAN;
            cbTuoShou.Checked = _YS_YG_YONGHUXX.I_SHOUFEIFS == 1;
            //取得财务部门信息
            DataRowCollection drc = _spHelper.P_YSYG_GETCWBMXX(_YS_YG_YONGHUXX.S_YONGHUH, null).Tables[0].Rows;
            if (drc.Count == 1)
            {
                txtCaiWuDZ.Text = Convert.ToString(drc[0]["S_DIZHI"]);
                txtCaiWuYB.Text = Convert.ToString(drc[0]["S_YOUBIAN"]);
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //当前无操作的数据则返回
            if (Status == GridViewStatus.Init)
            {
                return;
            }
            //修改操作
            operateState = OperateState.Modify;
            //取得当前操作的下月信息
            _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();
            _YS_YG_YONGHUXX.ID = YONGHUXXID;        
            _YS_YG_YONGHUXX.S_CAIWUDZ = txtCaiWuDZ.Text;
            _YS_YG_YONGHUXX.S_CAIWUYB = txtCaiWuYB.Text;
            _YS_YG_YONGHUXX.S_KAIHUHM = txtKaiHuHM.Text;
            _YS_YG_YONGHUXX.S_KAIHUYH = txtKaiHuYH.Text;
            _YS_YG_YONGHUXX.S_LIANXIBM = txtLianXiBM.Text;
            _YS_YG_YONGHUXX.S_LIANXIDH = txtLianXiDH.Text;
            _YS_YG_YONGHUXX.S_LIANXIR = txtLianXiR.Text;
            _YS_YG_YONGHUXX.S_YINHANGZH = txtYHZH.Text;
            _YS_YG_YONGHUXX.S_DIZHI = txtYongHuDZ.Text;
            _YS_YG_YONGHUXX.S_YONGHUH = txtYongHuH.Text;
            _YS_YG_YONGHUXX.S_YONGHUM = txtYONGHUM.Text;
            _YS_YG_YONGHUXX.S_YOUBIAN = txtYouBian.Text;
            _YS_YG_YONGHUXX.I_SHOUFEIFS = cbTuoShou.Checked ? 1 : 0;
            _YS_YG_YONGHUXX.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            _YS_YG_YONGHUXX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            //记忆一下用户号,保存后根据用户号定位数据
            string yonghuh = txtYongHuH.Text;
            string fieldList, queren, rsno, rsmsg;
            _YS_YG_YONGHUXXBLL.UpdateYS_YG_YONGHUXX(_YS_YG_YONGHUXX, null,out fieldList,out queren,out rsno,out rsmsg);
            if(rsno=="1")
            {
                _SYS_LOGBLL.Log<YS_YG_YONGHUXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "用户管理-银行资料", operateState, _YS_YG_YONGHUXX, null);
                //执行用户信息维护同步
                _spHelper.P_YS_YONGHUXXWEIHUTB(_YS_YG_YONGHUXX.S_YONGHUH, null,int.Parse(queren), LoginInfo.CurrentUser.UserID.ToString(), fieldList);
                //更新银行批量代理合同号
                _spHelper.P_YS_YINHANGPLDLHTHXG(_YS_YG_YONGHUXX.S_YONGHUH, txtHeTongH.Text);
                //更新用户财务信息
                _spHelper.P_YSYG_SETCWBMXX(_YS_YG_YONGHUXX.S_YONGHUH, _YS_YG_YONGHUXX.S_CAIWUDZ, _YS_YG_YONGHUXX.S_CAIWUYB);
                PageTools.AlertMsg(this, "保存成功！");
                Status = GridViewStatus.Init;
                Display();
                LoadData(false);
                YONGHUXXID = PageTools.FocusOpreateRecord(dtSource, gvList, "S_YONGHUH", yonghuh, true);
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
        }

        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            if (txtYONGHUM.Text == "")
            {
                PageTools.AlertMsg(this, "用户名不能为空！");
                txtYONGHUM.Focus();
                return false;
            }
            if (txtYongHuDZ.Text == "")
            {
                PageTools.AlertMsg(this, "用户地址不能为空！");
                txtYongHuDZ.Focus();
                return false;
            }
            if (txtYouBian.Text == "")
            {
                PageTools.AlertMsg(this, "邮编不能为空！");
                txtYouBian.Focus();
                return false;
            }
            if(txtLianXiR.Text=="")
            {
                PageTools.AlertMsg(this, "联系人不能为空！");
                txtLianXiR.Focus();
                return false;
            }
            if (txtLianXiDH.Text == "")
            {
                PageTools.AlertMsg(this, "联系电话不能为空！");
                txtLianXiDH.Focus();
                return false;
            }
            if (txtLianXiBM.Text == "")
            {
                PageTools.AlertMsg(this, "联系部门不能为空！");
                txtLianXiBM.Focus();
                return false;
            }
            //如果为托收,需要填写银行账号,开户银行,开户户名
            if (cbTuoShou.Checked)
            {
                if (txtYHZH.Text.Trim() == "")
                {
                    PageTools.AlertMsg(this, "银行账号不能为空！");
                    txtYHZH.Focus();
                    return false;
                }
                if (!PageTools.ValidateString(txtYHZH.Text, PageTools.eValidateType.数字))
                {
                    PageTools.AlertMsg(this, "请输入数字类型！");
                    txtYHZH.Focus();
                    return false;
                }
                if (txtKaiHuYH.Text.Trim() == "")
                {
                    PageTools.AlertMsg(this, "开户银行不能为空！");
                    txtKaiHuYH.Focus();
                    return false;
                }
                if (txtKaiHuHM.Text.Trim() == "")
                {
                    PageTools.AlertMsg(this, "开户户名不能为空！");
                    txtKaiHuHM.Focus();
                    return false;
                }
            }
            return true;
        }

        #endregion

    }
}